import { _decorator, Component, Node, sp } from 'cc';
import { GlobalConfig } from '../../../Init/Config/GlobalConfig';
import { GlobalEnum } from '../../../Init/Config/GlobalEnum';
import GlobalPool from '../../../Init/Tools/GlobalPool';
import { ArmyAnim } from './ArmyAnim';
import { MappingObj } from './MappingObj';
const { ccclass, property } = _decorator;

@ccclass('MappingArmy')
export class MappingArmy extends MappingObj {
    anim: ArmyAnim = null;
    curState: GlobalEnum.EnemyState = null;
    isShoot = false;

    init() {
        this.updateProp();
    }

    updateProp() {
        //回收
        if (!this.mappingProps.node.active) {
            GlobalPool.put(this.node);
            return;
        }

        //位置
        this.tmpP.set(this.mappingProps.node.position);
        this.tmpP.z = -this.tmpP.y;
        this.tmpP.y = 0;
        this.node.setPosition(this.tmpP.multiplyScalar(this.s3d));
        //旋转
        this.tmpR.x = 0;
        this.tmpR.z = 0;

        if (Math.abs(this.mappingProps.node.eulerAngles.x) == 180) {
            this.tmpR.y = -(this.mappingProps.node.eulerAngles.z % 360) + 180;
        } else {
            this.tmpR.y = this.mappingProps.node.eulerAngles.z;
        }
        this.tmpR.y += 90;

        this.node.eulerAngles = this.tmpR;

        //检查状态
        if (this.mappingProps.state !== null && (
            this.curState != this.mappingProps.state ||
            this.isShoot !== this.mappingProps.node[GlobalEnum.ExtralProp.ArmyAtk])) {
            this.curState = this.mappingProps.state;
            this.isShoot = this.mappingProps.node[GlobalEnum.ExtralProp.ArmyAtk];

            if (!this.anim) {
                this.anim = this.node.getComponent(ArmyAnim);
            }
            let clip;
            let spd = 1;
            if (this.curState == GlobalEnum.EnemyState.Alive) {
                if (this.isShoot) {
                    clip = GlobalEnum.ArmyClips.Shoot;
                    spd = GlobalConfig.Army[this.node.name].atkSpd;
                } else {
                    clip = GlobalEnum.ArmyClips.Idle;
                }

            } else {
                clip = GlobalEnum.ArmyClips.Death;
            }
            this.anim.playAnim(clip, false, spd);
        }
    }

}

